﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.NodeFactory;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using System.Xml;

namespace ExtendedWorkFlow.DataTypes
{

    [ToolboxData("<{0}:ExtendedWorkFlowGroupDocumentTypes runat=server></{0}:ExtendedWorkFlowGroupDocumentTypes>")]
    public class ExtendedWorkFlowGroupDocumentTypes : ListBoxMoveBase
    {

        #region Constructor

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.EnsureChildControls();

            this.AddClicked += new EventHandler(_Add_Click);
            this.RemoveClicked += new EventHandler(_Remove_Click);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Selected document types
        /// </summary>
        public string Selected
        {
            get
            {
                this.EnsureChildControls();

                List<string> colIds = new List<string>();

                foreach (ListItem objItem in this.Destination.Items)
                    colIds.Add(objItem.Value);

                return string.Join(",", colIds.ToArray());
            }
            set
            {
                this.EnsureChildControls();

                foreach (string strId in value.Split(",".ToCharArray()))
                {
                    if (umbraco.helper.IsNumeric(strId))
                        this.AddDocumentTypeToDestination(Convert.ToInt32(strId));
                }
            }
        }

        #endregion

        #region Child Controls

        protected override void ChildControlsCreated()
        {
            if (!this.Page.IsPostBack)
            {
                this.SetInitalSources();
            }
        }

        #endregion

        #region Document Types

        /// <summary>
        /// Get the inital document types and add them to the source listbox
        /// </summary>
        void SetInitalSources()
        {
            foreach (DocumentType objDocumentType in DocumentType.GetAllAsList())
                if (!objDocumentType.Text.StartsWith("ExtendedWorkFlow"))
                    this.Source.Items.Add(new ListItem(objDocumentType.Text, objDocumentType.Id.ToString()));
        }

        /// <summary>
        /// Add document type to destination listbox if not already in
        /// </summary>
        /// <param name="DocumentTypeId"></param>
        void AddDocumentTypeToDestination(int DocumentTypeId)
        {
            // If the document no longer exists it throws an exception catch it
            try
            {
                DocumentType objDocumentType = new DocumentType(DocumentTypeId);

                if (objDocumentType != null &&
                    objDocumentType.Id != 0)
                {
                    if (this.Destination.Items.FindByValue(DocumentTypeId.ToString()) == null)
                        this.Destination.Items.Add(new ListItem(objDocumentType.Text, objDocumentType.Id.ToString()));
                }
            }
            catch { }
        }

        #endregion

        #region Button Postbacks

        void _Remove_Click(object sender, EventArgs e)
        {
            List<ListItem> colRemove = new List<ListItem>();

            if (this.Destination.SelectedItem != null)
            {
                  // Loop though all selected items
                foreach (ListItem objListItem in this.Destination.Items)
                    if (objListItem.Selected)
                      colRemove.Add(objListItem);

                foreach (ListItem objListItem in colRemove)
                    this.Destination.Items.Remove(objListItem);
            }
        }

        void _Add_Click(object sender, EventArgs e)
        {
            if (this.Source.SelectedItem != null)
            {
                // Loop though all selected items
                foreach (ListItem objListItem in this.Source.Items)
                    if (objListItem.Selected)
                    {
                        this.AddDocumentTypeToDestination(Convert.ToInt32(objListItem.Value));
                    }
            }
        }

        #endregion

    }

}
